package com.ayou.session;

import com.ayou.executor.Executor;
import com.ayou.executor.SimpleExecutor;
import com.ayou.mapping.MappedStatement;
import com.ayou.plugin.InterptorChain;
import com.ayou.plugin.LimitInterceptor;
import com.ayou.plugin.SqlInterceptor;
import com.ayou.type.IntegerTypeHandler;
import com.ayou.type.StringTypeHandler;
import com.ayou.type.TypeHandler;
import com.google.common.collect.Maps;

import java.lang.reflect.Executable;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:  配置类  存放所有mysql的配置信息
 * @author: ayouman
 * @date: 2024/7/1 22:17
 */
public class Configuration {
    private Map<String, MappedStatement> mappedStatements = Maps.newHashMap();

    private Map<Class, TypeHandler> typeHandlerMap = Maps.newHashMap();

    private InterptorChain interptorChain = new InterptorChain();

    public Configuration() {
        typeHandlerMap.put(Integer.class, new IntegerTypeHandler());
        typeHandlerMap.put(String.class, new StringTypeHandler());

        interptorChain.addInterceptor(new LimitInterceptor());
    }

    public void addMappedStatement(MappedStatement mappedStatement) {
        mappedStatements.put(mappedStatement.getId(), mappedStatement);
    }

    public MappedStatement getMappedStatement(String id) {
        return mappedStatements.get(id);
    }

    public Map<Class, TypeHandler> getTypeHandlerMap() {
        return typeHandlerMap;
    }

    public Executor newExecutor() {
        return (Executor) interptorChain.pluginAll(new SimpleExecutor(this));
    }
}
